package cn.lhl.service.cargo;

import cn.lhl.dao.cargo.*;
import cn.lhl.domain.cargo.*;
import cn.lhl.domain.vo.ExportProductResult;
import cn.lhl.domain.vo.ExportResult;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@Service
public class ExportServiceImpl implements ExportService {

    @Autowired
    private ContractDao contractDao;

    @Autowired
    private ContractProductDao contractProductDao;

    @Autowired
    private ExportDao exportDao;

    @Autowired
    private ExportProductDao exportProductDao;

    @Autowired
    private ExtCproductDao extCproductDao;

    @Autowired
    private ExtEproductDao extEproductDao;

    public Export findById(String id) {
        Export export = exportDao.selectByPrimaryKey(id);
        return export;
    }

    public void save(Export export) {
        //此方法是生成报运单的过程
        //先将包运单中所有合同的状态赋成"0"即草稿
        //再将包运单中的货物和附件件数声明并赋初值
        int proNum = 0;
        int extNum = 0;

        //设置export的状态为0
        export.setState(0);

        //声明一个集合来存放报运单中的货物
        List<ExportProduct> exportProductList = new ArrayList<ExportProduct>();


        /*第一部分更新合同相关的信息*/
        //先从接收的参数中获得合同id(contract_id)的集合
        String contractIds = export.getContractIds();
        String[] contractIdList = contractIds.split(",");
        //循环得到合同id
        for (String s : contractIdList) {
            //得到每个id对应的合同实体类
            Contract contract = contractDao.selectByPrimaryKey(s);
            //设置每个合同的状态为已报运
            contract.setState(2);
            //更新合同
            contractDao.updateByPrimaryKeySelective(contract);
            //根据合同id查询得出对应的所有合同货物
            ContractProductExample cpExample = new ContractProductExample();
            ContractProductExample.Criteria cpExampleCriteria = cpExample.createCriteria();
            cpExampleCriteria.andContractIdEqualTo(contract.getId());
            List<ContractProduct> contractProductList = contractProductDao.selectByExample(cpExample);

            /*该阶段为更新合同货物并为报运字段中的货物赋值*/

            //循环遍历
            for (ContractProduct contractProduct : contractProductList) {
                //创建报运商品实体类
                ExportProduct exportProduct = new ExportProduct();
                //给报运商品实体类中赋值
                BeanUtils.copyProperties(contractProduct, exportProduct);
                //给报运商品实体类中的字段赋值
                //报运单id(在controller中已经赋值)
                exportProduct.setExportId(export.getId());
                //报运单货物id
                exportProduct.setId(UUID.randomUUID().toString());
                //将报运商品写入数据库中
                exportProductDao.insertSelective(exportProduct);
                //报运商品总件数
                proNum++;

                //将报运货物放入列表中
                exportProductList.add(exportProduct);

                //根据货物获得附件实体类
                ExtCproductExample extCproductExample = new ExtCproductExample();
                ExtCproductExample.Criteria extcExampleCriteria = extCproductExample.createCriteria();
                extcExampleCriteria.andContractProductIdEqualTo(contractProduct.getId());
                List<ExtCproduct> extCproductList = extCproductDao.selectByExample(extCproductExample);

                //创建报运附件实体类
                ExtEproduct extEproduct = new ExtEproduct();

                //遍历
                for (ExtCproduct extCproduct : extCproductList) {
                    BeanUtils.copyProperties(extCproduct, extEproduct);
                    //赋值
                    //报运附件ID
                    extEproduct.setId(UUID.randomUUID().toString());
                    //报运id
                    extEproduct.setExportId(export.getId());
                    //货物id
                    extEproduct.setExportProductId(extCproduct.getContractProductId());
                    //将其写入数据库中
                    extEproductDao.insertSelective(extEproduct);
                    //附件数量自增
                    extNum++;
                }

            }
        }
        //设置附件实体类
        export.setExtNum(extNum);
        export.setProNum(proNum);
        export.setExportProducts(exportProductList);
        exportDao.insertSelective(export);

    }

    public void update(Export export) {
        exportDao.updateByPrimaryKeySelective(export);

        if (export.getExportProducts() != null) {
            for (ExportProduct exportProduct : export.getExportProducts()) {
                exportProductDao.updateByPrimaryKeySelective(exportProduct);
            }
        }
    }

    public void delete(String id) {

    }

    public PageInfo findAll(ExportExample example, int page, int size) {
        PageHelper.startPage(page, size);
        List<Export> exportList = exportDao.selectByExample(example);
        return new PageInfo(exportList);
    }

    //电子报运完成更新数据库信息
    public void updateE(ExportResult exportResult) {
        //根据exportResult更新export实体类
        Export export = new Export();
        export.setId(exportResult.getExportId());
        export.setState(exportResult.getState());
        export.setRemark(exportResult.getRemark());
        //根据得到的products更新product
        //将export更新到数据库
        exportDao.updateByPrimaryKeySelective(export);
        Set<ExportProductResult> products = exportResult.getProducts();
        for (ExportProductResult product : products) {
            //4.更新exportProduct实体类
            ExportProduct exportProduct = new ExportProduct();
            exportProduct.setId(product.getExportProductId());
            exportProduct.setTax(product.getTax());
            //5.将exportProduct更新到数据库
            exportProductDao.updateByPrimaryKeySelective(exportProduct);
        }

    }
}
